<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Iteminvoicemapping
 *
 * @author Regan
 */
class Iteminvoicemapping extends AppModel {
    public function getDetailList($CAR)
    {
        $objlist = $this->query("select Serial, BrgUrai, JmlSat from tblpibdtl where CAR = '$CAR'");
        
        return $objlist;
    }
    
    public function getDataDetail($CAR)
    {
        $obj = $this->query("select a.CAR"
                . ", a.Serial"
                . ", a.BrgUrai"
                . ", a.JmlSat"
                . ", coalesce(b.DokNo, '') as DokNo"
                . ", coalesce(b.ProductLine, '') as ProductLine"
                . ", coalesce(b.ExternalProductId, '') as ExternalID "
                . "from tblpibdtl a "
                . "left join iteminvoicemappings b "
                . "on a.CAR = b.CAR "
                . "and a.Serial = b.Serial "
                . "where a.CAR = '$CAR'");
        
        return $obj;
    }
    
    public function RecordSave($detailsave)
    {
        $checkResult = $this->CheckExisting($detailsave);
        
        if($checkResult) //if true, berarti record sudah ada, metode update
        {
            $this->updateAll(
                    array(
                        'DokNo' => "'" . $detailsave['Iteminvoicemapping']['DokNo'] . "'",
                        'ProductLine' => "'" . $detailsave['Iteminvoicemapping']['ProductLine'] . "'" ,
                        'ExternalProductId' => "'" . $detailsave['Iteminvoicemapping']['ExternalProductId'] . "'" ,
                        'modifiedBy' => "'" . $detailsave['Iteminvoicemapping']['createdBy'] . "'",
                        'modified' => "'" . date('Y-m-d H:i:s') . "'"
                    ),
                    array(
                        'CAR' => $detailsave['Iteminvoicemapping']['CAR'],
                        'Serial' => $detailsave['Iteminvoicemapping']['Serial']
                    )
                );
        }
        else
        {
            $this->create();
            $this->save($detailsave);
        }
    }
    
    public function CheckExisting($detailsave)
    {
        $res = $this->find('all', array(
            'conditions' => array(
                'CAR' => $detailsave['Iteminvoicemapping']['CAR'],
                'Serial' => $detailsave['Iteminvoicemapping']['Serial']
                )
            ));
        
        if($res)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
